package async;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThenApplyDemo {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(100);
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int value = new Random().nextInt(100);
            System.out.println(value);
            return value;
        }, executor).thenApply(value -> {
            int result = value * 10;
            System.out.println(result);
            return result;
        });
        try {
            Integer result = future.get();
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
            future.cancel(true);
        } catch (ExecutionException e) {
            e.printStackTrace();
            future.cancel(true);
        } finally {
            executor.shutdown();
        }

    }
}
